@page "/"
@using MyBlogServer.Models
@using MyBlogServer.Services
@inject PostService PostService

@rendermode InteractiveServer

<PageTitle>首页</PageTitle>

<div class="blog-container">
    <h1>欢迎来到我的博客</h1>

    <div class="blog-content-wrapper">
        <div class="blog-posts-container">
            @if (!string.IsNullOrEmpty(errorMessage))
            {
                <div class="alert alert-danger">
                    <p>加载文章时出错: @errorMessage</p>
                </div>
            }
            else if (_posts != null && _posts.Any())
            {
                @foreach (var post in _posts)
                {
                    <article class="blog-post">
                        <h2>
                            <a href="/post/@post.Slug">@post.Title</a>
                        </h2>
                        <div class="meta">
                            <span>发布时间: @post.CreatedAt.ToString("yyyy年MM月dd日")</span>
                        </div>
                        <div class="summary">
                            @if (!string.IsNullOrEmpty(post.Summary))
                            {
                                @post.Summary
                            }
                            else
                            {
                                @((MarkupString)Markdig.Markdown.ToHtml(post.Content.Length > 200 ? post.Content.Substring(0, 200) + "..." : post.Content))
                            }
                        </div>
                        <div class="read-more">
                            <a href="/post/@post.Slug">阅读全文</a>
                        </div>
                        @if (!string.IsNullOrEmpty(post.Tags))
                        {
                            <div class="post-tags">
                                @foreach (var tag in post.Tags.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim()).Where(t => !string.IsNullOrEmpty(t)))
                                {
                                    <a href="/tag/@tag" class="tag">@tag</a>
                                }
                            </div>
                        }
                    </article>
                }
            }
            else if (!isLoading)
            {
                <div class="alert alert-info">
                    <p>暂无文章。</p>
                </div>
            }
            else
            {
                <div class="alert alert-info">
                    <p>正在加载文章...</p>
                </div>
            }
        </div>
        
        <aside class="tag-cloud-sidebar">
            <h3>标签云</h3>
            @if (!string.IsNullOrEmpty(tagErrorMessage))
            {
                <div class="alert alert-danger">
                    <p>加载标签时出错: @tagErrorMessage</p>
                </div>
            }
            else if (tags != null && tags.Any())
            {
                @foreach (var tag in tags)
                {
                    <a href="/tag/@tag" class="tag">@tag</a>
                }
            }
            else if (!isLoading)
            {
                <p>暂无标签</p>
            }
        </aside>
    </div>
</div>

@code {
    private IEnumerable<Post> _posts = new List<Post>();
    private IEnumerable<string> tags = new List<string>();
    private bool isLoading = true;
    private string? errorMessage;
    private string? tagErrorMessage;

    protected override async Task OnInitializedAsync()
    {
        try 
        {
            isLoading = true;
            errorMessage = null;
            tagErrorMessage = null;
            
            _posts = await PostService.GetAllPostsAsync();
        }
        catch (Exception ex)
        {
            errorMessage = ex.Message;
        }
        finally
        {
            isLoading = false;
        }
        
        // Load tags separately to avoid one failure affecting the other
        try 
        {
            tags = PostService.GetAllTags();
        }
        catch (Exception ex)
        {
            tagErrorMessage = ex.Message;
        }
    }
}